using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Data;
using System.Collections.ObjectModel;

namespace Code_Generator
{
	public class Setup
	{
		string m_ConnectionString;
		string m_EnumDescription;
		string m_SchemaName;
		string m_TableName;
		readonly ObservableCollection<string> m_TokenFields = new ObservableCollection<string>();
		readonly Dictionary<string, string> m_EnumFieldTypeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

		MetaData m_MetaData;

		public Setup()
		{
			SchemaName = "dbo";
			m_TokenFields.CollectionChanged += (s, e) => m_MetaData = null;
			//m_EnumFields.CollectionChanged += (s, e) => m_MetaData = null;

			CreatedDateField = "CreatedDate";
			CreatedByField = "CreatedByKey";

			UpdatedDateField = "UpdatedDate";
			UpdatedByField = "UpdatedByKey";
		}


		public string ClassName
		{
			get { return m_TableName; }
		}


		public string CreatedDateField { get; set; }
		public string CreatedByField { get; set; }


		public string UpdatedDateField { get; set; }
		public string UpdatedByField { get; set; }

		//public string CreatedDateField { get; set; }
		//public string CreatedByField { get; set; }

		public string ParameterName
		{
			get { return ClassName.Substring(0, 1).ToLower() + ClassName.Substring(1); }
		}


		public string ConnectionString
		{
			get { return m_ConnectionString; }
			set
			{
				m_ConnectionString = value;
				m_MetaData = null;
			}
		}

		internal MetaData MetaData
		{
			get { return m_MetaData; }
		}

		public string SchemaName
		{
			get { return m_SchemaName; }
			set
			{
				m_SchemaName = value;
				m_MetaData = null;
			}
		}

		public string TableName
		{
			get { return m_TableName; }
			set
			{
				m_TableName = value;
				m_MetaData = null;
			}
		}

		public string EnumDescription
		{
			get { return m_EnumDescription; }
			set
			{
				m_EnumDescription = value;
				m_MetaData = null;
			}
		}

		public bool AllowSelectAll { get; set; }
		public string DataContractNamespace { get; set; }
		public string Namespace { get; set; }
		public string TargetFolder { get; set; }

		internal DataTable ExecuteTable(string sqlFormat, params object[] args)
		{
			using (var con = OpenConnection())
			{
				using (var cmd = new SqlCommand(string.Format(sqlFormat, args), con))
				{
					using (var reader = cmd.ExecuteReader())
					{
						var dt = new DataTable();
						dt.Load(reader);
						return dt;
					}
				}
			}

		}

		internal SqlConnection OpenConnection()
		{
			var con = new SqlConnection(m_ConnectionString);
			con.Open();
			return con;
		}

		internal void PopulateMetaData()
		{
			if (m_MetaData == null)
				m_MetaData = MetaData.PopulateMetaData(this);
		}

		public string ToStringExpression { get; set; }

		public ObservableCollection<string> TokenFields
		{
			get { return m_TokenFields; }
		}

		public Dictionary<string, string> EnumFieldTypeMap
		{
			get { return m_EnumFieldTypeMap; }
		}

	}
}

